﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MineSweeperChallenge.Sweepers;
using System.Reflection;
using System.Windows.Media.Animation;

namespace MineSweeperChallenge
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Title += Assembly.GetExecutingAssembly().GetName().Version.ToString();
            cmbSweeper.SelectionChanged += (sender, e) =>
                {
                    lblCreatorOutput.Content = ((ISweeper)cmbSweeper.SelectedItem).Creator;
                    lblVersionOutput.Content = ((ISweeper)cmbSweeper.SelectedItem).Version;
                };
            cmbSweeper.Items.Add(new CompleteRandomSweeper());
            cmbSweeper.Items.Add(new SimpleSweeper());
            cmbSweeper.SelectedIndex = 0;
            cmbStrategy.Items.Add(new GameRunnerCreator<SingleThreadedGameRunner>() { Name = "Single threaded" });
            cmbStrategy.Items.Add(new GameRunnerCreator<MultiThreadedGameRunner>() { Name = "Multi-threaded using ThreadPool" });
            cmbStrategy.SelectedIndex = 1;
        }

        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            btnPlay.IsEnabled = btnPlayStepByStep.IsEnabled = false;

            ProgressBar lProgressBar = new ProgressBar()
            {
                IsIndeterminate = false,
                Orientation = Orientation.Horizontal,
                Width = 200,
                Height = 20,
                Minimum = 0,
                Maximum = 1
            };
            SBar.Items.Add(lProgressBar);

            ((GameRunnerCreator) cmbStrategy.SelectedItem).Create().RunGame((ISweeper)cmbSweeper.SelectedItem,
                int.Parse(tbxNumberOfGames.Text),
                int.Parse(tbxWidth.Text),
                int.Parse(tbxHeight.Text),
                int.Parse(tbxNumberOfMines.Text),
                pResult =>
                {
                    tbkResult.Text = pResult;
                    SBar.Items.Remove(lProgressBar);
                    btnPlay.IsEnabled = btnPlayStepByStep.IsEnabled = true;
                },
                lProgressBar);
        }

        private void btnPlayStepByStep_Click(object sender, RoutedEventArgs e)
        {
            new GameRunnerCreator<StepByStepPlayWindow>().Create().RunGame((ISweeper)cmbSweeper.SelectedItem,
                int.Parse(tbxNumberOfGames.Text),
                int.Parse(tbxWidth.Text),
                int.Parse(tbxHeight.Text),
                int.Parse(tbxNumberOfMines.Text),
                null,
                null);
        }
    }
}
